#include "VideoOcr.hpp"

CVideo::CVideo()
{
    m_MovieName = "";
    m_Inited = false;
    m_Width = 0;
    m_Height = 0;
    m_Duration = 0;
    m_log = "";
    m_Dir = "";
}

bool CVideo::OpenMovie(std::string csMovieName, int type)
{
    m_VC.open(csMovieName);
	bool res = m_VC.isOpened();
    if (res)
    {
        m_MovieName = csMovieName;

        m_origWidth = m_VC.get(cv::CAP_PROP_FRAME_WIDTH);
        m_origHeight = m_VC.get(cv::CAP_PROP_FRAME_HEIGHT);

        m_Width = m_origWidth;
        m_Height = m_origHeight;

        if ((m_origWidth == 0) || (m_origHeight == 0))
        {
            CloseMovie();
            return false;
        }

        /*if (m_origWidth > 1280)
         *		{
         *			double zoum = (double)1280 / (double)m_origWidth;
         *			m_Width = 1280;
         *			m_Height = (double)m_origHeight*zoum;
        }*/


        m_VC >> m_cur_frame;

        m_Pos = m_VC.get(cv::CAP_PROP_POS_MSEC);
        m_fps = m_VC.get(cv::CAP_PROP_FPS);
        m_frameNumbers = m_VC.get(cv::CAP_PROP_FRAME_COUNT);
        m_Duration = -1;

        if (m_frameNumbers > 0)
        {
            m_Duration = ((m_frameNumbers * 1000.0) / m_fps);
        }

        m_ImageGeted = true;

    }
    return res;
}
bool CVideo::CloseMovie()
{
    if (m_VC.isOpened())
    {
		m_VC.release();
		return true;

    }
	return false;
}
void CVideo::OneStep()
{
    if (m_VC.isOpened())
    {
        s64 prevPos, curPos;
        s64 prevNumFrameToBeDecoded, curNumFrameToBeDecoded;
        int num_tries = 0;

        do
        {
            prevPos = m_VC.get(cv::CAP_PROP_POS_MSEC);
            prevNumFrameToBeDecoded = m_VC.get(cv::CAP_PROP_POS_FRAMES);
            m_VC >> m_cur_frame;
            curPos = m_VC.get(cv::CAP_PROP_POS_MSEC);
            curNumFrameToBeDecoded = m_VC.get(cv::CAP_PROP_POS_FRAMES);
            num_tries++;

            if ((curPos != prevPos) || (curNumFrameToBeDecoded != prevNumFrameToBeDecoded))
            {
                num_tries = 0;
            }
        } while ((m_cur_frame.empty()) && ((curPos != prevPos) || (curNumFrameToBeDecoded != prevNumFrameToBeDecoded) || (num_tries < 100)));

        m_Pos = curPos;

        if (!m_cur_frame.empty())
        {
            m_ImageGeted = true;

        }else{
            m_Pos = m_Duration;
        }
    }
}
/////////////////////////////////////////////////////////////////////////////

void CVideo::SetPos(double Pos)
{
    if (m_VC.isOpened())
    {

        double dt = 1000.0 / m_fps;

        if ((Pos < m_Pos) && (Pos > (m_Pos - dt * 3 / 2)))
        {
            double pos = m_VC.get(cv::CAP_PROP_POS_FRAMES);
            m_VC.set(cv::CAP_PROP_POS_FRAMES, pos-2);
            OneStep();
            double cur_pos = m_VC.get(cv::CAP_PROP_POS_FRAMES);
            cur_pos = cur_pos;
        }
        else
        {
            if (m_frameNumbers > 0)
            {
                m_VC.set(cv::CAP_PROP_POS_FRAMES, (Pos * m_frameNumbers) / m_Duration);
                OneStep();
            }
        }
    }
}
bool CVideo::GetImage(cv::Rect rec){
    if (!m_cur_frame.empty())
    {
		if (rec.x > m_Width)
		{
			printf("rec.x Exceeding image size!");
			return false;
		}
		if (rec.y > m_Height)
		{
			printf("rec.y Exceeding image size!");
			return false;
		}
		if ((rec.x + rec.width) > m_Width){
			rec.width=m_Width-rec.x;
		}
		if ((rec.y + rec.height) > m_Height){
			rec.height=m_Height-rec.y;
		}
		m_rec_image=m_cur_frame(rec);
		return true;
	}else{
		return false;
		
	}
}

